package com.usian.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.ResultVo;
import com.usian.student.dao.StudentDao;
import com.usian.student.feign.ClassFeignService;
import com.usian.student.pojo.TbClass;
import com.usian.student.pojo.TbStudent;
import com.usian.student.service.StudentService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, TbStudent> implements StudentService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassFeignService classFeignService;
    @Override
    public ResultVo register(TbStudent tbStudent) throws IOException {
        //判断三要素是否为同一个人姓名+身份证号+手机号
//        VerifyRealName verifyRealName=new VerifyRealName();
//        Map resuly=verifyRealName.checkUser(tbStudent.getCardNo(),tbStudent.getName(),tbStudent.getPhone());
//        System.out.println("返回值"+resuly);
//        if (resuly.get("code").equals("200")){
        if (true){
            //请求接口成功
//            if (resuly.get("msg").equals("1")){
            if (true){
                //信息一致
                //判断手机验证码是否正确,判断用户输入验证码和redis中的验证码是否一致
                /*
                验证码流程
                        1.验证redis中验证码是否过期
                        2.验证用户输入的验证码和redis验证码是否一致
                */
                String rediscode=stringRedisTemplate.opsForValue().get(tbStudent.getPhone());
                if (!StrUtil.isEmpty(rediscode)){
                    if (rediscode.equals(tbStudent.getCode())){
                        //判断重复注册
                        LambdaQueryWrapper<TbStudent> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                        lambdaQueryWrapper.eq(TbStudent::getPhone,tbStudent.getPhone());
                        long count=studentDao.selectCount(lambdaQueryWrapper);
                        if (count>0){
                            //该手机号注册
                            return new ResultVo(300,"该手机号已注册，不能重复注册",null);
                        }else {
                            //需要根据所在班级id来获取对应的项目经理id和班主任id
                            ResultVo resultVo=classFeignService.info(tbStudent.getClassId());
                            TbClass tbClass=JSON.parseObject(JSON.toJSONString(resultVo.getData()),TbClass.class);
                            tbStudent.setTId(tbClass.getTId());
                            tbStudent.setHId(tbClass.getHId());
                            //用户未注册保存到数据库
                            studentDao.insert(tbStudent);
                            return new ResultVo(200,"success",null);
                        }
                    }else {
                        return new ResultVo(300,"验证码输入错误",null);
                    }
                }else {
                    return new ResultVo(300,"验证码过期",null);
                }
                //判断重复注册
            }else {
//                return new ResultVo(Integer.parseInt(resuly.get("code")+""),resuly.get("msg")+"",null);
                return new ResultVo(300,"异常",null);
            }
        }else {
            //请求接口异常
//            return new ResultVo(Integer.parseInt(resuly.get("code")+""),resuly.get("msg")+"",null);
            return new ResultVo(300,"异常",null);
        }
        //判断手机验证码是否正确

        //判断重复代码
    }
}
